﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;
using mesoBoard.Data;
using mesoBoard.Data.Repositories;
using System.Drawing.Imaging;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using mesoBoard.Common;

namespace mesoBoard.Services
{
    public class Search : ISearch
    {
         
        private IRepositoriesWrapper Repositories;

        public Search(IRepositoriesWrapper repos)
        {
            this.Repositories = repos;
        }

        public List<SearchResult> Lookup(string text, SearchType type)
        {
            text = text.ToLower();

            if (type == SearchType.PostsByUser || type == SearchType.ThreadsByUser)
                return ByUser(text, type);
            else
                return ByKeywords(text, type);
        }

        public List<SearchResult> ByUser(string Username, SearchType type)
        {
            User TheUser = Repositories.Users.GetUser(Username.ToLower());

            if (TheUser == null)
                return null;

            IEnumerable<Post> posts;
            List<SearchResult> ret = new List<SearchResult>();

            IEnumerable<Post> allPosts = Repositories.Posts.GetAllPosts();
            IEnumerable<Thread> allThreads = Repositories.Threads.GetAllThreads();

            if (type == SearchType.PostsByUser)
                posts = allPosts.Where(x => x.UserID == TheUser.UserID).AsEnumerable();
            else
                posts = allThreads.Where(x => x.FirstPost.UserID == TheUser.UserID).Select(x => x.FirstPost).AsEnumerable();

            posts = posts.Distinct();

            foreach (Post p in posts)
            {
                ret.Add(new SearchResult
                {
                    ThreadTitle = p.Thread.FirstPost.Title,
                    PostID = p.PostID,
                    ThreadID = p.ThreadID,
                    Text = p.Text
                });

            }

            return ret;
        }

        public List<SearchResult> ByKeywords(string Keywords, SearchType type)
        {
            string[] split = Keywords.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            List<SearchResult> ret = new List<SearchResult>();

            IEnumerable<Post> allPosts = Repositories.Posts.GetAllPosts();

            foreach (string s in split)
            {
                IEnumerable<Post> results;
                if (type == SearchType.Text)
                    results = allPosts.Where(x => x.Text.ToLower().Contains(s));
                else if (type == SearchType.Title)
                    results = allPosts.Where(x => !string.IsNullOrWhiteSpace(x.Title)).Where(x => x.Title.ToLower().Contains(s));
                else
                {
                    results = allPosts.Where(x => x.Text.ToLower().Contains(s) || (string.IsNullOrWhiteSpace(x.Title) ? false : x.Title.ToLower().Contains(s)));
                }

                allPosts = allPosts.Distinct();

                foreach (Post p in results)
                {
                    ret.Add(new SearchResult
                    {
                        ThreadTitle = p.Thread.FirstPost.Title,
                        PostID = p.PostID,
                        ThreadID = p.ThreadID,
                        Text = p.Text
                    });

                }
            }
            return ret;
        }

    }
}